<!DOCTYPE html>
<uieip type="page" value="login" debug="true"></uieip>
<uieip type="reg" value="console"></uieip>
<html>

<head>
    <uieip type="file" value="inc" src="/yqwin/easyui/include.txt"></uieip>
    <uieip type="file" value="css" src="/yqwin/css/yqwin.min" minute="10"></uieip>
    <uieip type="file" value="js" src="/yqwin/src/yqwin.min" minute="10"></uieip></uieip>
    <title>Data Edit</title>
</head>

<body>
    <div style="margin:3px 0 3px 3px">
        <select id="cg_app" style="width: 300px;"></select>
    </div>
    <ul id="tt_menu">
    </ul>
    <script>
        var dbname = "yqbase";

        function form_load() {
            $(window).resize(function () {
                onresize();
            });
            onresize();

            var options = {
                ajaxUrl: "_db.html"
            };

            $("#cg_app").yqComboGrid("loadData");
        }

        function onresize() {
            var w = window.innerWidth;
            var h = window.innerHeight;
            $('#cg_app').yqComboGrid("resize", {
                width: w - 6
            });

            $('#tt_menu').yqTree("resize", {
                width: w - 1,
                height: h - 36
            });
        }

        $(function () {
            $.yqDbList({ ajaxUrl: "_db.html" },
                function (data) {
                    if (data.error == 0) {
                        dbname = data.rows[0].name;
                        console.log(data, dbname);
                        init_app();
                    } else {
                        alert(data.info);
                    }
                });
        });

        function init_app() {
            $("#cg_app").yqComboGrid({
                ajaxUrl: "/_db.html",
                dbname: dbname,
                tbname: "sys_menu_app",
                keyId: "sid",
                displayMember: "{name}[{no}]",

                titles: [],
                loadData: false,
                isEdit: true,
                isSeek: false,

                isPage: false,

                move: true,

                editMode: 2,
                editModeW: 600,
                editModeH: 450,
                editModeNo: -1,

                afterBindingTitle: function () {
                    init_menu();
                },

                onChange: function (newValue, newRow, oldValue, oldRow) {
                    //console.log(newValue,oldValue);
                    var v = $(this).yqComboGrid("getValue");
                    //console.log("getValue",v);

                    if (newValue == null) {
                        $('#tt_menu').yqTree("setBindingValue", null);
                        $('#tt_menu').yqTree("clear");
                    }
                    else {
                        $('#tt_menu').yqTree("setBindingValue", newValue);
                        $('#tt_menu').yqTree("loadData");
                    }
                },
            });
        }

        function init_menu() {
            $('#tt_menu').yqTree({
                ajaxUrl: "/_db.html",
                dbname: dbname,
                tbname: "sys_menu",
                keyId: "sid",
                bindingKeyId: "app_id",
                bindingValue: null,
                pKeyId: "pid",
                pKeyValue: null,

                loadDataType: 2,
                displayMember: "{name}[{no}]",
                loadData: false,

                menuType: 3,

                overflowY: true,

                editMode: 2,
                editModeW: 600,
                editModeH: 450,
                editModeNo: -1,

                editMenus: [{
                    id: "link",
                    text: "菜单设置",
                    iconCls: "icon2-link",
                }],
                editLinks_r: [{
                    id: "link_r",
                    text: "菜单设置",
                    iconCls: "icon2-link",
                }],
                uppActionState: function (s_node, callback) {
                    var menu_type = 0;
                    if (s_node != null) {
                        menu_type = s_node.attributes["menu_type"];
                        if (menu_type === undefined || menu_type == null) {
                            menu_type = 0;
                        }
                    }

                    if (menu_type == 1 || menu_type == 2) {
                        $('#tt_menu').yqTree("btnEnable", "link");
                        $('#tt_menu').yqTree("btnEnable", "link_r");
                    } else {
                        $('#tt_menu').yqTree("btnDisable", "link");
                        $('#tt_menu').yqTree("btnDisable", "link_r");
                    }

                    callback(true);
                },
                btnAction: function (action,params, callback) {
                    if (action == "link" || action == "link_r") {
                        var node = $(this).yqTree("getSelected");
                        if (node != null) {
                            treeDblClick(node);
                        }
                        callback(false);
                    } else {
                        callback(true);
                    }
                },
                beforeBindingTitle: function (titles, callback) {
                    //console.log("beforeBindingTitle", titles);
                    callback(titles);
                },
                afterBindingTitle: function () {
                    form_load();
                },
                beforeSetdata: function (data, callback) {
                    for (var i = 0; i < data.length; i++) {
                        var icon = data[i].icon;
                        if (!(icon === undefined || icon === null)) {
                            var filevalue = $.getFileLink(icon);
                            var cssName = "icon" + icon.sid.replace(/-/g, "");
                            var cssText = '.' + cssName + '{background:url(\'' + filevalue + '\') no-repeat center center;}';
                            $.yqAddCSS(cssText, cssName);
                            data[i].iconCls = cssName;
                        }
                    }
                    //console.log("beforeSetdata", data);
                    callback(data)
                },
                beforeEditNodeToUI: function (node) {    //节点编辑后加载前node处理
                    var icon = node.attributes.icon;
                    if (!(icon === undefined || icon === null)) {
                        var filevalue = $.getFileLink(icon);
                        var cssName = "icon" + icon.sid.replace(/-/g, "");
                        var cssText = '.' + cssName + '{background:url(\'' + filevalue + '\') no-repeat center center;}';
                        $.yqAddCSS(cssText, cssName);
                        node.iconCls = cssName;
                    } else {
                        node.iconCls = null;
                    }
                    return node;
                },
                afterSetdata: function (data) {
                    console.log("afterSetdata", data);
                },
                formatter: function (node) {
                    //menu_type 0 文件夹,1 菜单界面,2 弹出界面
                    var menu_type = node.attributes["menu_type"];
                    if (menu_type === undefined || menu_type == null) {
                        menu_type = 0;
                    }
                    var text = node.text;
                    if (node.text == null) {
                        text = node.attributes["describe"];
                    }

                    return text;
                },
                onDblClick: function (node) {
                    if (node == null) {
                        return;
                    }

                    treeDblClick(node);
                },
            });

            function treeDblClick(node) {
                var menu_type = node.attributes["menu_type"];
                if (menu_type === undefined || menu_type == null) {
                    menu_type = 0;
                }
                if (!(menu_type == 1 || menu_type == 2)) {
                    return;
                }

                window.parent.$MidWin.yqFormMdi("editwin", {
                    id: node.attributes["sid"],
                    title: (node.text == null ? node.attributes["name"] : node.text),
                    openmore: false,
                    content: "",
                    url: "/adp/menuedit.html?dbname=" + dbname + "&menu_id=" + node.id,
                    closable: true,
                });
            }
        }
    </script>
</body>

</html>